import {
  VuexModule,
  Module,
  Action,
  Mutation,
  getModule,
} from "vuex-module-decorators";
import { login, logout, getUserInfo } from "@/api/users";
import { getToken, setToken, removeToken, setCookie, getCookie } from "@/utils/cookies";
import store from "@/store";

export interface IUserState {
  token: string;
  name: string;
  avatar: string;
  introduction: string;
  rolesId: any;
  user: any
  roles: string[];
  //病人所属科室
  depts: string[];
}

@Module({ dynamic: true, store, name: "user" })
class User extends VuexModule implements IUserState {
  public token = getToken() || "";
  public name = "";
  public avatar = "";
  public introduction = "";
  public rolesId = "";
  public user = "";
  public roles: string[] = [];
  public depts: string[] = []
  @Mutation
  private SET_TOKEN(token: string) {
    this.token = token;
  }

  @Mutation
  private SET_NAME(name: string) {
    this.name = name;
  }

  @Mutation
  private SET_AVATAR(avatar: string) {
    this.avatar = avatar;
  }

  @Mutation
  private SET_INTRODUCTION(introduction: string) {
    this.introduction = introduction;
  }

  @Mutation
  private SET_ROLESID(rolesId: any) {
    this.rolesId = rolesId;
  }
  @Mutation
  private SET_USER(user: any) {
    this.user = user;
  }
  @Mutation
  private SET_DEPTS(depts: any) {
    this.depts = depts;
  }
  @Mutation
  private SET_ROLES(roles: string[]) {
    this.roles = roles;
  }

  @Action
  public async Login(userInfo: { username: string; password: string }) {
    let { username, password } = userInfo;
    username = username.trim();
    const { data } = await login({ username, password });
    setToken(data.accessToken);
    this.SET_TOKEN(data.accessToken);
    await this.GetUserInfo();
  }
  @Action
  public GetUsername2depts() {
    try {
      return JSON.parse(<string>getCookie('uname2depts'));
    } catch (error) {

    }
    return {};

  }

  @Action
  public ResetToken() {
    removeToken();
    this.SET_TOKEN("");
    this.SET_ROLES([]);
  }

  @Action
  public async GetUserInfo() {
    if (this.token === "") {
      throw Error("GetUserInfo: token is undefined!");
    }
    const { data } = await getUserInfo({
      /* Your params here */
    });
    if (!data) {
      throw Error("Verification failed, please Login again.");
    }
    const { roles, name, avatar, introduction, rolesId, user, depts } = data;
    // roles must be a non-empty array
    if (!roles || roles.length <= 0) {
      throw Error("GetUserInfo: roles must be a non-null array!");
    }
    this.SET_ROLES([rolesId]);
    this.SET_NAME(name);
    setCookie('uname2depts', JSON.stringify(depts));
    setCookie('username', name);
    this.SET_AVATAR(avatar);
    this.SET_INTRODUCTION(introduction);
    this.SET_ROLESID(rolesId);
    this.SET_USER(user);
    this.SET_DEPTS(depts)
  }

  @Action
  public async LogOut() {
    if (this.token === "") {
      throw Error("LogOut: token is undefined!");
    }

    //TODO:暂时去掉 await logout()
    removeToken();
    this.SET_TOKEN("");
    this.SET_ROLES([]);
  }
}

export const UserModule = getModule(User);
